//
//  WebViewVC.m
//  mmda
//
//  Created by 李陆涛 on 2017/9/10.
//  Copyright © 2017年 mmdatv. All rights reserved.
//

#import "WebViewVC.h"
#import <WebKit/WebKit.h>
#import "UIView+Gradient.h"

@interface WebViewVC () <WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler>

@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic) NSInteger res;

@end

@implementation WebViewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup];
}

- (void)setup {
    
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    [config.userContentController addScriptMessageHandler:self name:@"imguider"];
    
    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) configuration:config];
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    [self.view addSubview:self.webView];
    self.webView.scrollView.scrollEnabled = NO;
    
    self.barBGAlpha = 0;
    
    [self rightBarButtonWithImageName:@"close" selector:@selector(backAction)];
    
    [ActivityIndicatorView showHUDAddedTo:self.view backgroundColor:kBGColor];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
    
    if (@available(iOS 11.0, *)) {
        self.webView.scrollView.contentInsetAdjustmentBehavior =UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self setNavGradient];
}

- (void)backAction {
    
    if (self.res <= 0) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        return;
    }
    
    [IGAlertView alertWithTitle:nil message:[NSString localizedStringWithFormat:MYLocalizedString(@"HOMEPAGE_ASK_QUESTION_EXIT", nil),@(self.res)] cancelButtonTitle:MYLocalizedString(@"HOMEPAGE_ASK_QUESTION_EXITCONFORM", nil) commitBtn:MYLocalizedString(@"HOMEPAGE_ASK_QUESTION_EXITCANCEL", nil) commit:nil cancel:^{
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    
    NSDictionary *obj = message.body;
    
    NSString *strSel = obj[@"method"];
    NSDictionary *param = obj[@"params"];
    
    if (strSel && param.count > 0) {
        
        strSel = [strSel stringByAppendingString:@":"];
    }
    
    SEL sel = NSSelectorFromString(strSel);
    
    if (sel && [self respondsToSelector:sel]) {
        
        [self performSelector:sel withObject:param];
    }
}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
    
    // 禁用选中效果
    [self.webView evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none'" completionHandler:nil];
    [self.webView evaluateJavaScript:@"document.documentElement.style.webkitTouchCallout='none'" completionHandler:nil];
}

- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation {
    
    
    [ActivityIndicatorView hideHUDAddedTo:self.view];
}

- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {

    [ActivityIndicatorView hideHUDAddedTo:self.view];
}


#pragma mark -webinterface

- (void)completequestionnaire {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)restq:(NSDictionary *)params {
    
    NSInteger rest = [params[@"rest"] integerValue];
    self.res = rest;
}

@end
